<?php

namespace Addons\SchoolNews\Controller;
use DiDom\Document;
use Home\Controller\AddonsController;
use Org\Util\Snoopy;

class SchoolNewsController extends AddonsController{
    public function _initialize()
    {
        vendor("DiDom.Document");
        vendor("DiDom.Query");
        vendor("DiDom.Element");
    }

    public function index()
    {
        $NewsList = array(
            array(
                "title" => "校园要闻",
                "href" => 22
            ), array(
                "title" => "校园通知",
                "href" => 2
            ), array(
                "title" => "公示公告",
                "href" => 3
            ), array(
                "title" => "社会热点",
                "href" => 24
            ), array(
                "title" => "系部动态",
                "href" => 113
            ),
        );
        $this->assign("NewsList",$NewsList);
        $this->assign("sliderImg", $this->getImg());
        $this->assign("SchoolNews", $this->getSchoolNews());
        $this->assign("SchoolNotice", $this->getSchoolNotice());
        $this->assign("GsGg", $this->getGsGg());
        $this->assign("ShRd", $this->getShRd());
        $this->assign("XbDt", $this->getXbDt());

        $this->display();
    }

    private function getNewsList($url)
    {
        $sn = new Snoopy();
        $sn->fetch($url);
        $document = new Document($sn->results);
        $posts = $document->find('.listbox2 .view-content tr');
        foreach ($posts as $post) {
            $rows[] = array(
                "href" => preg_replace('/\D/s', '', $post->find('a')[0]->href),
                "title" => $post->find('a')[0]->text(),
                "date"=>$post->find('.date-display-single')[0]->text()

            );
        }
        return $rows;
    }


    private function getSchoolNews()
    {
        return $this->getNewsList("http://gxsdxy.cn/catalog-list/96/22");
    }

    private function getSchoolNotice()
    {
        return $this->getNewsList("http://gxsdxy.cn/catalog-list/96/2");
    }

    private function getGsGg()
    {
        return $this->getNewsList("http://gxsdxy.cn/catalog-list/96/3");
    }

    private function getShRd()
    {
        return $this->getNewsList("http://gxsdxy.cn/catalog-list/96/24");
    }

    private function getXbDt()
    {
        return $this->getNewsList("http://gxsdxy.cn/catalog-list/96/113");
    }


    private function getNewsContent($href)
    {
        $url = "http://gxsdxy.cn" . $href;
        $sn = new Snoopy();
        $sn->fetch("http://gxsdxy.cn/content/5940");
        $document = new Document($this->formaturl($sn->results, "http://gxsdxy.cn/"));
        $posts = $document->find('.node-content img')[0];
    }

    public function getNewsImg($href)
    {
        $url = "http://gxsdxy.cn" . $href;
        $sn = new Snoopy();
        $sn->fetch($url);
        $document = new Document($this->formaturl($sn->results, "http://gxsdxy.cn/"));
        $posts = $document->find('.node-content img')[0];
        return $posts->src;
    }

    public function getSimpleNews()
    {
        $sn = new Snoopy();
        $sn->fetch('http://gxsdxy.cn/content/5940');
        $sn->expandlinks = true;
        $document = new Document($sn->results);
        $posts = $document->find('.node-content')[0]->text();
        return mb_substr($posts, 0, 26);
    }



    private function getImg()
    {
        $sn = new Snoopy();
        $sn2 = new Snoopy();
        $sn->fetch('http://gxsdxy.cn');

        $document = new Document($this->formaturl($sn->results, "http://gxsdxy.cn/"));

        $posts = $document->find('#views_slideshow_cycle_main_article_index_box-panel_pane_8 .views_slideshow_slide');
        foreach ($posts as $post) {
            $sn2->fetch($post->find('img')[0]->src);
            $rows[] = array(

                "href" => preg_replace('/\D/s', '', $post->find('.views-field-field-image a')[0]->href),
                "title" => $post->find('.views-field-title a')[0]->title,
                "src" => base64_encode($sn2->results)
            );

        }

//        dump($rows);
        return $rows;
    }
    

    
    public function formaturl($l1,$l2){
        if (preg_match_all("/(<img[^>]+src=\"([^\"]+)\"[^>]*>)|(<a[^>]+href=\"([^\"]+)\"[^>]*>)|(<img[^>]+src='([^']+)'[^>]*>)|(<a[^>]+href='([^']+)'[^>]*>)/i",$l1,$regs)){
            foreach($regs[0] as $num => $url){
                $l1 = str_replace($url,$this->lIIIIl($url,$l2),$l1);
            }
        }
        return $l1;
    }
    public function lIIIIl($l1,$l2){
        if(preg_match("/(.*)(href|src)\=(.+?)( |\/\>|\>).*/i",$l1,$regs)){$I2 = $regs[3];}
        if(strlen($I2)>0){
            $I1 = str_replace(chr(34),"",$I2);
            $I1 = str_replace(chr(39),"",$I1);
        }else{return $l1;}
        $url_parsed = parse_url($l2);
        $scheme = $url_parsed["scheme"];if($scheme!=""){$scheme = $scheme."://";}
        $host = $url_parsed["host"];
        $l3 = $scheme.$host;
        if(strlen($l3)==0){return $l1;}
        $path = dirname($url_parsed["path"]);if($path[0]=="\\"){$path="";}
        $pos = strpos($I1,"#");
        if($pos>0) $I1 = substr($I1,0,$pos);
//判断类型
        if(preg_match("/^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+/i",$I1)){return $l1; }//http开头的url类型要跳过
        elseif($I1[0]=="/"){$I1 = $l3.$I1;}//绝对路径
        elseif(substr($I1,0,3)=="../"){//相对路径
            while(substr($I1,0,3)=="../"){
                $I1 = substr($I1,strlen($I1)-(strlen($I1)-3),strlen($I1)-3);
                if(strlen($path)>0){
                    $path = dirname($path);
                }
            }
            $I1 = $l3.$path."/".$I1;
        }
        elseif(substr($I1,0,2)=="./"){
            $I1 = $l3.$path.substr($I1,strlen($I1)-(strlen($I1)-1),strlen($I1)-1);
        }
        elseif(strtolower(substr($I1,0,7))=="mailto:"||strtolower(substr($I1,0,11))=="javascript："){
            return $l1;
        }else{
            $I1 = $l3.$path."/".$I1;
        }
        return str_replace($I2,"\"$I1\"",$l1);
    }

    public function index2()
    {
        $dom = new \DOMDocument();


//        $sn=new Snoopy();
//        $sn->fetch('http://gxsdxy.cn');
//        $document = new Document($sn->results);
        $document = new Document();
        echo $document->loadHtmlFile('http://www.example.com/');
        $document->getDocument();
//        $document = new Document(formaturla("http://gxsdxy.cn",$sn->results));
        $posts = $document->find('#views_slideshow_cycle_main_article_index_box-panel_pane_8 .views_slideshow_slide');
//        $posts->node->childNodes;
//        echo $posts[0]->html();
//dump($posts);
        foreach ($posts as $post) {
//            $post->baseURI="http://gxsdxy.cn";
//            echo $post->find('.views-field-field-image a')[0]->href. "\n";
//            echo $post->find('.views-field-title a')[0]->title. "\n";
//            echo $post->find('img')[0]->src. "<br>";
        }

    }

    public function post()
    {
        vendor("DiDom.Document");
        vendor("DiDom.Query");
        vendor("DiDom.Element");
        vendor("DiDom");
        $dom = new \DOMDocument();


        $sn = new Snoopy();
        $sn->fetch('http://gxsdxy.cn/content/5940');
        $sn->expandlinks = true;
        $document = new Document($this->formaturl($sn->results, "http://gxsdxy.cn/"));
//        $document = new Document(formaturla("http://gxsdxy.cn",$sn->results));
        $posts = $document->find('.node-content');
//        $posts->node->childNodes;
//        echo $posts[0]->html();

        foreach ($posts as $post) {
//            $post->baseURI="http://gxsdxy.cn";
            echo $post->html() . "\n";
        }
//dump($posts) ;

//        $this->display();

//        vendor("HtmlParser.ParserDom");
//        $sn=new Snoopy();
//        $sn->fetch('http://www.thinkphp.cn/');
//        $html=new ParserDom($sn->results);
//        $ret = $html->find('ul li');
//        dump($ret);
    }

    public function listnews()
    {
        vendor("DiDom.Document");
        vendor("DiDom.Query");
        vendor("DiDom.Element");
        vendor("DiDom");
        $dom = new \DOMDocument();


        $sn = new Snoopy();
        $sn->fetch('http://gxsdxy.cn/catalog-list/96/1');
        $sn->expandlinks = true;
        $document = new Document($sn->results);
//        $document = new Document(formaturla("http://gxsdxy.cn",$sn->results));
        $posts = $document->find('.listbox2 .view-content a');
//        $posts->node->childNodes;
//        echo $posts[0]->html();
//dump($posts);
        foreach ($posts as $post) {
//            $post->baseURI="http://gxsdxy.cn";
            echo $post->href . "\n";
            $rows[] = array($post->href, $post->text());
        }
        dump($rows);
    }
}
